#include "component.h"
#include "cetele.h"
#include "form.h"

component::component(int x,int y,int pid,std::string ptip) : Eleman(x,y,40,40)
{
    box(FL_NO_BOX);
    cid=pid;
    cTip=ptip;
    cStatus=0;
    cAngel=0;
    aci=cAngel*15;
    cSelectable=true;
    cDeletable=true;
    cMovable=true;
    cFromLink1=true;
    cFromLink2=true;
    cToLink1=true;
    cToLink2=true;
    resim=NULL;
    xx=0;
    yy=0;
    sildelete=false;
    nitwid=new comProWidget(650,60,cx,cy,pid,ptip,this);
    nitwid->_wStatus_(status());
    nitwid->_wDeletable_(deletable());
    nitwid->_wMovable_(movable());
    nitwid->_wSelectable_(selectable());
    nitwid->_wAngel_(angel());
    nitwid->_wFromLink1_(fromLink1());
    nitwid->_wFromLink2_(fromLink2());
    nitwid->_wToLink1_(toLink1());
    nitwid->_wToLink2_(toLink2());
    nitwid->end();
    nx = cos(aci*15*(22/7.0)/180)*22;
    ny = sin(aci*15*(22/7.0)/180)*22;

    sol=new Node(20-nx+x-6,20-ny+y-4,pid);
    sag=new Node(20+nx+x-4,20+ny+y-4,pid+1);

    sag->movable(false);
    sol->movable(false);

    sag->deletable(false);
    sol->deletable(false);

    sag->nitelikGoster(false);
    sol->nitelikGoster(false);

    sag->toLink(toLink2());
    sol->toLink(toLink1());

    sag->fromLink(fromLink2());
    sol->fromLink(fromLink1());

    sol->position(sol->x()+sol->w()/2,sol->y()+sol->h()/2);
    sag->position(sag->x()+sag->w()/2,sag->y()+sag->h()/2);

    ustbilgi=new Fl_Box(this->x()-20,this->y()-20,this->w()+40,20);
    ustbilgi->box(FL_PLASTIC_DOWN_BOX);
    ustbilgi->color(FL_YELLOW);
}

component::~component()
{
    delete resim;
}
void component::draw()
{
    Fl_Button::draw();
    resim->draw(x()+4,y()+4,32,32,xx,yy);
}
int component::handle(int e)
{
    Cetele *p=(Cetele *)parent();
    Form *pp=(Form *)p->parent()->parent();
    comProWidget *yedek;
    Node *gecici;
    component *cgecici;
    std::string bilgi;
    static int butx=0;
    static int buty=0;
    switch(e)
    {
        case FL_PUSH:
            butx=x()-Fl::event_x();
            buty=y()-Fl::event_y();
            p->add(sag);
            p->add(sol);
            bilgi="X:";
            bilgi+=p->sirala(x())+" Y:"+p->sirala(y()-60+h()/2);
            ustbilgi->label(bilgi.c_str());
            p->add(ustbilgi);
            break;
        case FL_DRAG:
            if(Fl::event_button1())
            {
                int xx1=round(butx+Fl::event_x());
                int yy1=round(buty+Fl::event_y());
                if(xx1>0 && xx1<650 && yy1>70 && yy1<510)
                {
                    position(xx1,yy1);
                    nx = cos(aci*15*(22/7.0)/180)*22;
                    ny = sin(aci*15*(22/7.0)/180)*22;

                    sol->position(20-nx+x()-6,20-ny+y()-4);
                    sag->position(20+nx+x()-4,20+ny+y()-4);
                    ustbilgi->position(xx1-20,yy1-20);
                    p->add(ustbilgi);
                    pp->redraw();
                    for(int i=0;i<p->tumnodetut.size();i++)
                    {
                        gecici=p->tumnodetut[i];
                        if(gecici!=sol && gecici!=sag && gecici->y()-5<this->y()+h()/2 && gecici->y()+5>this->y()+h()/2)
                        {
                            this->position(x(),gecici->y()-h()/2+4);
                            sol->position(20-nx+x()-6,20-ny+y()-4);
                            sag->position(20+nx+x()-4,20+ny+y()-4);
                            fl_line(this->x(),y()+h()/2,gecici->x(),y()+h()/2);
                            fl_line_style(FL_SOLID,2,"");
                            fl_color(FL_DARK_GREEN);
                        }
                    }
                    for(int i=0;i<p->compTut.size();i++)
                    {
                        cgecici=p->compTut[i];
                        if(cgecici!=this && cgecici->y()-5<this->y() && cgecici->y()+5>this->y())
                        {
                            this->position(x(),cgecici->y());
                            sol->position(20-nx+x()-6,20-ny+y()-4);
                            sag->position(20+nx+x()-4,20+ny+y()-4);
                            fl_line(this->x(),y(),cgecici->x(),y());
                            fl_line_style(FL_SOLID,2,"");
                            fl_color(FL_DARK_GREEN);
                        }
                        if(cgecici!=this && cgecici->x()-5<this->x() && cgecici->x()+5>this->x())
                        {
                            this->position(cgecici->x(),y());
                            sol->position(20-nx+x()-6,20-ny+y()-4);
                            sag->position(20+nx+x()-4,20+ny+y()-4);
                            fl_line(x(),this->y(),x(),cgecici->y());
                            fl_line_style(FL_SOLID,2,"");
                            fl_color(FL_DARK_GREEN);
                        }
                    }
                    for(int i=0;i<sag->gidencizgitut.size();i++)
                    {
                        sag->gidencizgitut[i]->cizdir1();
                    }
                    for(int i=0;i<sag->gelencizgitut.size();i++)
                    {
                        sag->gelencizgitut[i]->cizdir2();
                    }
                    for(int i=0;i<sol->gidencizgitut.size();i++)
                    {
                        sol->gidencizgitut[i]->cizdir1();
                    }
                    for(int i=0;i<sol->gelencizgitut.size();i++)
                    {
                        sol->gelencizgitut[i]->cizdir2();
                    }
                    bilgi="X:";
                    bilgi+=p->sirala(x())+" Y:"+p->sirala(y()-60+h()/2);
                    ustbilgi->label(bilgi.c_str());
                    nitwid->pozisyon(x(),y()-60+h()/2);
                    nitwid->redraw();
                }
            }
            break;
        case FL_FOCUS:
            nitwid->_wStatus_(status());
            nitwid->_wDeletable_(deletable());
            nitwid->_wMovable_(movable());
            nitwid->_wSelectable_(selectable());
            nitwid->_wAngel_(angel());
            nitwid->_wFromLink1_(fromLink1());
            nitwid->_wFromLink2_(fromLink2());
            nitwid->_wToLink1_(toLink1());
            nitwid->_wToLink2_(toLink2());
            pp->remove(pp->prop());
            pp->add(nitwid);
            pp->prosecim(nitwid);
            pp->redraw();
            pp->textX(0);
            pp->textY(0);
            pp->textW(0);
            pp->textH(0);
            pp->textR(0);
            pp->textG(0);
            pp->textB(0);
            pp->textAlpha(0);
            pp->textScrollv(0);
            pp->textScrollh(0);
            pp->textPage()->deactivate();
            pp->altpro()->value(pp->genelPage());
            break;
        case FL_UNFOCUS:
            p->remove(ustbilgi);
            break;
        case FL_KEYDOWN:
            if(Fl::event_key()==FL_Delete)
                sildelete=true;
            break;
    }

    return Fl_Button::handle(e);
}

std::string component::tipgonder()
{
    return "component";
}

int component::id()
{
    return cid;
}
std::string component::type()
{
    return cTip;
}
void component::status(int pStatus)
{
    cStatus=pStatus;
}
int component::status()
{
    return cStatus;
}
void component::selectable(bool pSelectable)
{
    cSelectable=pSelectable;
}
bool component::selectable()
{
    return cSelectable;
}
void component::movable(bool pMovable)
{
    cMovable=pMovable;
}
bool component::movable()
{
    return cMovable;
}
void component::fromLink1(bool pFromLink)
{
    cFromLink1=pFromLink;
}
void component::fromLink2(bool pFromLink)
{
    cFromLink2=pFromLink;
}
bool component::fromLink1()
{
    return cFromLink1;
}
bool component::fromLink2()
{
    return cFromLink2;
}
void component::toLink1(bool pToLink)
{
    cToLink1=pToLink;
}
void component::toLink2(bool pToLink)
{
    cToLink2=pToLink;
}
bool component::toLink1()
{
    return cToLink1;
}
bool component::toLink2()
{
    return cToLink2;
}
void component::deletable(bool pDeletable)
{
    cDeletable=pDeletable;
}
bool component::deletable()
{
    return cDeletable;
}
void component::value(int pValue)
{
    cValue=pValue;
}
int component::value()
{
    return cValue;
}

void component::angel(int p)
{
    cAngel=p;
}
int component::angel()
{
    return cAngel;
}
void component::resimbelirle(std::string path)
{
    if(resim)
        delete resim;
    resim=new Fl_PNG_Image(path.c_str());
    this->redraw();
}
Node *component::saggonder()
{
    return sag;
}
Node *component::solgonder()
{
    return sol;
}
comProWidget * component::Nitelik()
{
    return nitwid;
}
void component::nodesCalc(int mAngel)
{
    aci=mAngel;

    nx = cos(aci*15*(22/7.0)/180)*22;
    ny = sin(aci*15*(22/7.0)/180)*22;

    sol->position(20-nx+x()-6,20-ny+y()-6);
    sag->position(20+nx+x()-4,20+ny+y()-6);
    Fl_Gel_Tabs *p=(Fl_Gel_Tabs *)parent()->parent();
    for(int i=0;i<sag->gidencizgitut.size();i++)
    {
        sag->gidencizgitut[i]->cizdir1();
    }
    for(int i=0;i<sag->gelencizgitut.size();i++)
    {
        sag->gelencizgitut[i]->cizdir2();
    }
    for(int i=0;i<sol->gidencizgitut.size();i++)
    {
        sol->gidencizgitut[i]->cizdir1();
    }
    for(int i=0;i<sol->gelencizgitut.size();i++)
    {
        sol->gelencizgitut[i]->cizdir2();
    }
    yy=floor((aci)/5)*32;
    xx=(aci%5)*32;
    this->redraw();
    p->redraw();

}
bool component::deletesil()
{
    return sildelete;
}
int component::koordx()
{
    return cx;
}
int component::koordy()
{
    return cy;
}
std::string component::item_id()
{
    return cItem_id;
}
void component::item_id(std::string p)
{
    cItem_id=p;
}
